package com.qf.controller;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/send")
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //
    @GetMapping("/sendMessage")
    public String sendMessage(){


        //交换机名称不一样
        String exchangeName = "topic_exchange";

        //参数1：交换机
        //参数2： RoutingKey 关键词   决定了由哪个消费者所消费
        //参数3： 发送的数据
        rabbitTemplate.convertAndSend(exchangeName,
                "apple.orange.banana","主题模式的消息测试数据apple.orange.banana");
        rabbitTemplate.convertAndSend(exchangeName,
                "lazy.haha.hehe.hoho.ooo","主题模式的消息测试数据lazy.haha.hehe.hoho.ooo");
        rabbitTemplate.convertAndSend(exchangeName,
                "redis.es.rabbit","主题模式的消息测试数据redis.es.rabbit");

        return "OK";
    }

    /**
     * 本次发消息  是为了测试或验证 避免消息重新被消费
     */
    @GetMapping("/sendRetryMessag")
    public String sendRetryMessage(){

        //UUID  本次发送消息的唯一主键
        CorrelationData  correlationData = new CorrelationData(UUID.randomUUID().toString());
        //交换机名称不一样
        String exchangeName = "boot_topic_exchange";
        rabbitTemplate.convertAndSend(exchangeName,
                "redis.es.rabbit","测试避免重复处理消息",correlationData);

        return "OK";
    }
}
